﻿using Application.MainModule.CreditCardManagement;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Domain.MainModule.CreditCards;
using Domain.MainModule.Entities;
using System.Collections.Generic;
using Infraestructure.CrossCutting.IoC;

namespace Application.MainModule.Tests
{
    /// <summary>
    ///This is a test class for CreditCardManagementServiceTest and is intended
    ///to contain all CreditCardManagementServiceTest Unit Tests
    ///</summary>
    [TestClass()]
    public class CreditCardManagementServiceTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        /// <summary>
        ///A test for AddCreditCard
        ///</summary>
        [TestMethod()]
        public void AddCreditCard_Invoke_Test()
        {
            //Arrange
            ICreditCardManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<ICreditCardManagementService>();
            Guid creditCardId = Guid.Parse("11112222-1234-AABB-AAFF-AAAACCBBBBB9");
            CreditCard creditCard = new CreditCard()
            {
                CreditCardId = creditCardId,                
            };

            //Act
            target.AddCreditCard(creditCard);
            CreditCard result = target.FindCreditCardById(creditCardId);

            //Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(creditCard, result);
            target.RemoveCreditCard(result);
        }

        /// <summary>
        ///A test for ChangeCreditCard
        ///</summary>
        [TestMethod()]
        public void ChangeCreditCard_Invoke_Test()
        {
            //Arrange
            ICreditCardManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<ICreditCardManagementService>();
            Guid creditCardId = Guid.Parse("11111111-1234-AABB-AAFF-AAAACCBBBBBB");
 
            //Act
            CreditCard creditCard = target.FindCreditCardById(creditCardId);
            creditCard.Number = "modified";
            target.ChangeCreditCard(creditCard);
            CreditCard result = target.FindCreditCardById(creditCardId);

            //Assert
            Assert.IsNotNull(result);
            Assert.AreEqual("modified", result.Number);
        }

		/// <summary>
        ///A test for FindCreditCardById
        ///</summary>
        [TestMethod()]
        public void FindCreditCardById_Invoke_Test()
        {
            //Arrange
            ICreditCardManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<ICreditCardManagementService>();
            Guid creditCardId = Guid.Parse("11112222-1234-AABB-AAFF-AAAACCBBBBBB");

            //Act 
            CreditCard creditCard = target.FindCreditCardById(creditCardId);

            //Assert
            Assert.IsNotNull(creditCard);
            Assert.AreEqual(creditCardId, creditCard.CreditCardId);
        }
		
        /// <summary>
        ///A test for FindCreditCardByUserId
        ///</summary>
        [TestMethod()]
        public void FindCreditCardByUserId_Invoke_Test()
        {
            //Arrange
            ICreditCardManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<ICreditCardManagementService>();
            Guid userId = Guid.Parse("21111111-1234-AABB-AAFF-AAAACCBBBBBB");

            //Act
            List<CreditCard> actual;
            actual = target.FindCreditCardByUserId(userId);

            //Assert
            Assert.IsNotNull(actual);
            Assert.AreEqual(1, actual.Count);
            Assert.AreEqual(userId, actual[0].UserId);
        }

        [TestMethod()]
        [ExpectedException(typeof(ArgumentNullException))]
        public void RemoveCreditCard_Invoke_NullItemThrowNewArgumentNullException_Test()
        {
            //Arrange
            ICreditCardManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<ICreditCardManagementService>();

            //Act
            target.RemoveCreditCard(null);
        }

        /// <summary>
        ///A test for RemoveCreditCard
        ///</summary>
        [TestMethod()]
        public void RemoveCreditCard_Invoke_Test()
        {
            //Arrange
            ICreditCardManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<ICreditCardManagementService>();
            CreditCard creditCard = null;

            //Act
            creditCard = target.FindCreditCardById(Guid.Parse("11111111-1234-AABB-AAFF-AAAACCBBBBBB"));
            target.RemoveCreditCard(creditCard);

            //Assert
            CreditCard result = target.FindCreditCardById(Guid.Parse("11111111-1234-AABB-AAFF-AAAACCBBBBBB"));
            Assert.IsNull(result);
            creditCard = new CreditCard()
                    {
                        CreditCardId = Guid.Parse("11111111-1234-AABB-AAFF-AAAACCBBBBBB"),
                        Number = "123456",
                        UserId = Guid.Parse("21111111-1234-AABB-AAFF-AAAACCBBBBBB")
                    };
            target.AddCreditCard(creditCard);
        }
    }
}
